﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace HSIA
{
    public class AmateParam
    {

        /// <summary>
        /// 自定义常量  
        /// </summary>
        /// <returns></returns>
        public class Units
        {
            /// <summary>
            /// 速度键值，默认0 
            /// </summary>
            /// <returns></returns>
            public static readonly string S = "S";
            /// <summary>
            /// 航向键值，默认1 
            /// </summary>
            /// <returns></returns>
            public static readonly string A = "A";
            /// <summary>
            /// 电量键值，默认2 
            /// </summary>
            /// <returns></returns>
            public static readonly string B = "B";
            /// <summary>
            /// 信号键值，默认3 
            /// </summary>
            /// <returns></returns>
            public static readonly string Sa = "S";
            /// <summary>
            /// 卫星键值，默认4 
            /// </summary>
            /// <returns></returns>
            public static readonly string Sl = "Sl";

            /// <summary>
            /// 油量键值，默认8  
            /// </summary>
            /// <returns></returns>
            public static readonly string FL = "FL";
            /// <summary>
            /// 开关门键值，默认9  
            /// </summary>
            /// <returns></returns>
            public static readonly string D = "PARA_3";
            /// <summary>
            /// 湿度门键值，默认10  
            /// </summary>
            /// <returns></returns>
            public static readonly string H = "PARA_2";
            /// <summary>
            /// 温度键值，默认11  
            /// </summary>
            /// <returns></returns>
            public static readonly string T = "PARA_1";
            /// <summary>
            /// 温度键值，默认11  
            /// </summary>
            /// <returns></returns>
            public static readonly string Sb = "Sb";

            public static string[] Names = (PublicFunc.sLanguage == "CN") ? new string[] { "速度", "航向", "电量", "信号", "卫星", "油量", "门禁", "防盗", "湿度", "温度" } :
                                                                            new string[] { "Speed", "Azimuth", "Battery", "Signal", "Satellite", "Fuel", "Door", "Burglar", "Humi", "Temp" };
            public static string[] IDs = new string[] { S, A, B, Sa, Sl, FL, D, Sb, H, T };
            public static string[] sUnit = new string[] { "Km/h", "°", "%", "C", "L", "O/C", "%rh", "℃" };
            public static string[] Tags = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8" };
            public static string[] Modes = (PublicFunc.sLanguage == "CN") ? new string[] { "数字", "数组", "图/文" } :
                                                                            new string[] { "Number", "Array", "photograph" };

            public static double dLat = (PublicFunc.sLanguage != "CN") ? 0 : 0.0024;
            public static double dLng = (PublicFunc.sLanguage != "CN") ? 0 : 0.0055;

            //public static void GetUnits(DatabaseObject dbo)
            //{
            //    T_u = dbo.GetMeasureName(Ti, 2);
            //    H_u = dbo.GetMeasureName(Hi, 2);
            //    B_u = dbo.GetMeasureName(Bi, 2);
            //    FL_u = dbo.GetMeasureName(FLi, 2);
            //    S_u = dbo.GetMeasureName(Si, 2);
            //}

            /// <summary>
            /// 正常数据键值，默认0 
            /// </summary>
            /// <returns></returns>
            public static string sNormal = "0";
            /// <summary>
            /// (高)报警数据键值，默认1 
            /// </summary>
            /// <returns></returns>
            public static string sAboved = "1";
            /// <summary>
            /// (高)预报警数据键值，默认2 
            /// </summary>
            /// <returns></returns>
            public static string sAbove = "2";
            /// <summary>
            /// (低)预报警数据键值，默认3 
            /// </summary>
            /// <returns></returns>
            public static string sBelow = "3";
            /// <summary>
            /// (低)报警数据键值，默认4 
            /// </summary>
            /// <returns></returns>
            public static string sBelowd = "4";
            /// <summary>
            /// (高参数)报警数据键值，默认5 
            /// </summary>
            /// <returns></returns>
            public static string sUReference = "5";
            /// <summary>
            /// (低参数)报警数据键值，默认6 
            /// </summary>
            /// <returns></returns>
            public static string sLReference = "6";

        }

        /// <summary>
        /// 自定义外部参数  
        /// </summary>
        /// <returns></returns>
        public class Configuration
        {
            static DataTable dtSystem;

            public static string strFile = System.Windows.Forms.Application.StartupPath + "\\Amate.xml";

            static string sConn { get; set; }

            public static void Open()
            {
                dtSystem = PublicFunc.readXML(strFile, "System");
                if (dtSystem == null || !dtSystem.Columns.Contains("Name") || !dtSystem.Columns.Contains("Value"))
                {
                    dtSystem = new DataTable();
                    dtSystem.TableName = "System";
                    dtSystem.Columns.Add("Name", System.Type.GetType("System.String"));
                    dtSystem.Columns.Add("Value", System.Type.GetType("System.String"));
                }
                dtSystem.PrimaryKey = new DataColumn[] { dtSystem.Columns["Name"] };
            }

            public static object GetSystemValue(string sName, object oDefaultValue)
            {
                object oRet = null;
                DataRow drSystemParam = dtSystem.Rows.Find(sName);  //"PosPrintFilter"
                if (drSystemParam != null)
                {
                    oRet = drSystemParam["Value"];
                }
                else
                {
                    dtSystem.LoadDataRow(new object[] { sName, oDefaultValue }, true);
                    oRet = oDefaultValue;
                }
                return oRet;
            }

            public static void SetSystemValue(string sName, string sValue)
            {
                DataRow drSystemParam = dtSystem.Rows.Find(sName);  //"PosPrintFilter"
                if (drSystemParam != null)
                {
                    drSystemParam["Value"] = sValue;
                }
                else
                {
                    dtSystem.LoadDataRow(new object[] { sName, sValue }, true);
                }
            }

            public static string GetConnString(string strKey, string sInitialCatalog)
            {
                if (strKey == "") strKey = "20010206";
                string sServer = GetSystemValue("DataSource", ".\\sqlexpress").ToString();
                if (sInitialCatalog == "") 
                { 
                    sInitialCatalog = GetSystemValue("InitialCatalog", PublicFunc.sDatabase).ToString(); 
                }
                string sUserID = PublicFunc.GetUniformStr(GetSystemValue("UserID", "").ToString(), strKey);
                string sPassword = PublicFunc.GetUniformStr(GetSystemValue("Password", "").ToString(), strKey);
                string strConn = "Data Source=" + sServer + ";Initial Catalog=" + sInitialCatalog + ";";
                if (sUserID != "" && sPassword != "")
                {
                    strConn += "Integrated Security=false;User ID=" + sUserID + ";Password=" + sPassword;
                }
                else strConn += "Integrated Security=SSPI;";
                return strConn;
            }

            public static void Save()
            {
                try
                {
                    if (dtSystem != null) dtSystem.WriteXml(strFile);
                }
                catch { }
            }

        }
    }

}
